<?php
namespace app\index\controller;

use app\common\controller\Frontend;
use think\Config;
use think\Cookie;
use think\Hook;
use think\Db;
use think\Session;
use think\Validate;
use think\Request;
use fast\Random;
/**
 * 适岗测评测试中心
 */
class Suitable extends Frontend
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    protected $model = null;
    protected $type_model = null;
    protected $leader_model = null;
    protected $assesslog_model = null;


    public function _initialize()
    {
        parent::_initialize(); 
        $this->leader_model = new \app\admin\model\suitable\Leader;
        $this->type_model = new \app\admin\model\suitable\Type;
        $this->model = new \app\admin\model\suitable\Handler;
        $this->assesslog_model = new \app\admin\model\suitable\Assesslog;
    }

    public function reading($code = null){
        $this -> view -> engine -> layout("layout/suitable");
        $row = $this->model->where('code',$code)->find();
        if(empty($row) || $row['status']!='normal'){
            $this->error('二维码有误,请重新扫码');
        }
        $log = $this->assesslog_model->where('handler_id',$row['id'])->count();
        $isset_log = $log??0;
        $this -> assign('isset_log',$isset_log);
        $this -> assignconfig('code',$code);
        $this -> assign('lz_ceping_xuzhi',config('site.sg_ceping_xuzhi'));

        $this -> assignconfig('sg_start',strtotime(config('site.sg_start'))*1000);
        $this -> assign('sg_start',config('site.sg_start'));
        $this -> assign('sg_end',config('site.sg_end'));
        $is_sg = 0;
        if(time()>strtotime(config('site.sg_end'))){
            $is_sg = 2;//结束
        }
        if(time()<strtotime(config('site.sg_start'))){
            $is_sg = 1;//还未开始
        }
        $this -> assign('is_sg',$is_sg);
        $this -> assignconfig('sg_end',strtotime(config('site.sg_end'))*1000);
        
        $this -> assign('titles','适岗测评');

        return $this->view->fetch('layout/reading');
    }


    public function index($code = null){
        $this -> view -> engine -> layout("layout/suitable");
        // $row = $this->model->where('code',$code)->find();
        // if(empty($row) || $row['status']!='normal'){
        //     $this->error('二维码有误,请重新扫码');
        // }
        // $log = $this->assesslog_model->where('handler_id',$row['id'])->find();
        // $isset_log = $log??0;
        // $this -> assign('isset_log',$isset_log);
        // $this -> assign('code',$code);

        // $this -> assignconfig('sg_ceping_xuzhi',config('site.sg_ceping_xuzhi'));

        return $this->view->fetch();
    }

    //统一测评
    public function vocation($code = null,$type = null){
        $row = $this->model->where('code',$code)->find();
        ini_set('max_input_vars','10000');
        $this -> view -> engine -> layout("layout/suitable");

        $subjects = $this->leader_model->where('status','normal')->order('id asc')->select();
        $pagearr= [];
        foreach ($subjects as $key => $value) {
            $subjects[$key]['typelist'] = $this->type_model->where('status','normal')->order('id asc')->select();
            $pagearr[$key+1] = [];
        }
        $count = count($subjects);
        if($this -> request -> isPost()){
            $answers = $this -> request -> post('answer_arr/a');
            if($answers)
            {
                foreach ($subjects as $key => $value) {
                    $zonghesuzhi = '';
                    $yewunengli = '';
                    $lvzhijinze = '';
                    $banzijianshe = '';
                    $shengtaifenwei = '';
                    $gongzuoshiji = '';
                    foreach ($answers[$value['id']] as $k => $v){
                        if($value['id']==$v['id'] && $v['type']=='zonghesuzhi'){
                            $zonghesuzhi = $v['answer'];
                        }
                        if($value['id']==$v['id'] && $v['type']=='yewunengli'){
                            $yewunengli = $v['answer'];
                        }
                        if($value['id']==$v['id'] && $v['type']=='lvzhijinze'){
                            $lvzhijinze = $v['answer'];
                        }
                        if($value['id']==$v['id'] && $v['type']=='banzijianshe'){
                            $banzijianshe = $v['answer'];
                        }
                        if($value['id']==$v['id'] && $v['type']=='shengtaifenwei'){
                            $shengtaifenwei = $v['answer'];
                        }
                        if($value['id']==$v['id'] && $v['type']=='gongzuoshiji'){
                            $gongzuoshiji = $v['answer'];
                        }
                    }
                    $data = [
                        'code'=>$row['code'],
                        'handler_id'=>$row['id'],
                        'handler_name'=>$row['name'],
                        'leader_id'   => $value['id'],
                        'leader_name'   => $value['name'],
                        'zonghe_suzhi'   => $zonghesuzhi,
                        'yewu_nengli'   => $yewunengli,
                        'lvzhi_jinze'   => $lvzhijinze,
                        'banzi_jianshe'   => $banzijianshe,
                        'shengtai_fenwei'   => $shengtaifenwei,
                        'gongzuo_shiji'   => $gongzuoshiji,
                        'content'=> serialize($answers),
                        'create_time'=> time(),
                        'update_time'=> time(),
                    ];
                    $this->assesslog_model -> insert($data);
                }
                unset($v);
            }
            $this -> success();
        }

        $this -> assignconfig('count',$count);
        $this -> assignconfig('pagearr',$pagearr);
        $this -> assign('count',$count);
        $this -> assign('subjects',$subjects);
        

        $this -> assign('code',$code);
        $this -> assignconfig('code',$code);

        $this -> assignconfig('type',$type);

        $this -> assignconfig('sg_ceping_neirong',config('site.sg_ceping_neirong'));
        return $this->view->fetch();
    }

    public function person($code = null){
        $this -> view -> engine -> layout("layout/suitable");
        $subjects = $this->leader_model->order('id asc')->select();
        $this -> assignconfig('subjects',$subjects);
        $this -> assignconfig('code',$code);
        return $this->view->fetch();
    }


}